// // timeFormat.js
 
// function padZero(num) {
//   return num < 10 ? '0' + num : num;
// }
  
//  function timeFormat(date, fmt) {
//    const options = {
//      'yyyy': date.getFullYear(),
//      'mm': padZero(date.getMonth() + 1),
//      'dd': padZero(date.getDate()),
//      'HH': padZero(date.getHours()),
//      'MM': padZero(date.getMinutes()),
//      'ss': padZero(date.getSeconds()),
//      // 可以根据需要添加更多格式选项
//    };
  
//    let result = '';
//    for (let i = 0; i < fmt.length; i++) {
//      const char = fmt[i];
//      if (options[char]) {
//        result += options[char];
//      } else {
//        result += char;
//      }
//    }
//    return result;
//  }
// // function timeFormat(date, format) {
// //   const options = {
// //     yyyy: date.getFullYear(),
// //     mm: padZero(date.getMonth() + 1), // 月份从0开始，需要加1
// //     dd: padZero(date.getDate()),
// //     // 你可以根据需要添加更多的格式化选项，例如小时、分钟、秒等
// //     // hh: padZero(date.getHours()),
// //     // mm: padZero(date.getMinutes()), // 注意：这里和月份的mm冲突，需要区分上下文或使用不同的标识符
// //     // ss: padZero(date.getSeconds()),
// //   };
 
// //   // 使用正则表达式替换格式字符串中的占位符
// //   return format.replace(/(yyyy|mm|dd)/g, (match) => options[match]);
// // }
 
// // 导出函数，以便在其他文件中使用
// module.exports = timeFormat;


// timeFormat.js
 
/**
 * Pads a single digit number with a leading zero if necessary.
 * @param {number} num - The number to pad.
 * @returns {string} - The padded string.
 */
function padZero(num) {
  return num < 10 ? '0' + num : num.toString();
}
 
/**
 * Formats a date object into a specified string format.
 * @param {Date} date - The date object to format.
 * @param {string} fmt - The format string.
 * @returns {string} - The formatted date string.
 */
function timeFormat(date, fmt) {
  // Map of format characters to their corresponding date methods
  const formatMap = {
    'yyyy': date.getFullYear(),
    'mm': padZero(date.getMonth() + 1), // Months are zero-based
    'dd': padZero(date.getDate()),
    'HH': padZero(date.getHours()),
    'MM': padZero(date.getMinutes()),
    'ss': padZero(date.getSeconds()),
    // Add more format options as needed
  };
 
  let result = '';
  for (let i = 0; i < fmt.length; i++) {
    const char = fmt[i];
    // Check if the current character is a format placeholder
    if (formatMap[char]) {
      result += formatMap[char];
    } else if (char === '/') {
      // If the character is a slash (or any other delimiter), add it directly
      result += char;
    } else {
      // If it's not a format placeholder or delimiter, it's part of the literal format string
      // (this could be used for adding text between date parts, e.g., "yyyy-mm-dd")
      // However, in this implementation, we assume non-mapped chars are typos or mistakes,
      // so we can optionally throw an error or just ignore them and add them to the result.
      // For simplicity, we'll just add them to the result here.
      result += char;
      // Alternatively, you could throw an error:
      // throw new Error(`Unknown format character: ${char}`);
    }
  }
  return result;
}
 
// Example usage (not part of the function definition):
// const formattedDate = timeFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
// console.log(formattedDate); // Outputs something like "2023-04-01 12:34:56"
 
// Export the function so it can be used in other files (if using ES6 modules)
export default timeFormat;